package com.atguigu.cmsservice.controller;


import com.atguigu.cmsservice.entity.CrmBanner;
import com.atguigu.cmsservice.entity.vo.BannerVo;
import com.atguigu.cmsservice.service.CrmBannerService;
import com.atguigu.commonutils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author wangshuai
 * @since 2021-03-04
 */
@RestController
@RequestMapping("/cmsservice/banner")
@CrossOrigin
public class CrmBannerController {

    @Autowired
    private CrmBannerService bannerService;

    @GetMapping("getAllBanner")
    public R getAllBanner() {

        List<Map> list = bannerService.bannerList();

        return R.ok().data("data",list);
    }

    @PostMapping("bannerPageList/{page}/{limit}")
    public R bannerPageList(@PathVariable Long page, @PathVariable Long limit, @RequestBody(required = false) BannerVo bannerVo) {

        Map<String,Object> map = bannerService.showBannerList(page,limit,bannerVo);

        return R.ok().data("data",map);
    }

    @GetMapping("getBannerById/{id}")
    public R getBannerById(@PathVariable String id) {

        CrmBanner banner = bannerService.getById(id);

        return R.ok().data("data",banner);
    }

    @PostMapping("saveBanner")
    public R saveBanner(@RequestBody CrmBanner banner) {

        bannerService.save(banner);

        return R.ok();
    }

    @PostMapping("updateBanner")
    public R updateBanner(@RequestBody CrmBanner banner) {

        Boolean flag = bannerService.updateById(banner);

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @DeleteMapping("deleteBatchBanner")
    public R deleteBatchBanner(@RequestParam String ids) {

        String[] idArr = ids.split(",");

        List<String> idList = new ArrayList<>();

        Collections.addAll(idList,idArr);

        Boolean flag = bannerService.removeByIds(idList);

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

